package com.util;

import java.io.*;
import java.net.*;
 


public class UrlTool { 
	
		public static String httpPost(String POST_URL,String pramStr){
			String backContent=null;
			
	        try {
				URL postUrl = new URL(POST_URL);
				HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();
				connection.setDoOutput(true);
				connection.setDoInput(true);
				connection.setRequestMethod("POST");
				connection.setUseCaches(false);
				connection.setInstanceFollowRedirects(true);
				connection.setConnectTimeout(5000);
				connection.setReadTimeout(5000);
				connection.connect();
				
				DataOutputStream out = new DataOutputStream(connection.getOutputStream());
				out.writeBytes(pramStr);
				out.flush();
				out.close();
				BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),"GB2312"));
				String line;
				 
				StringBuilder sb = new StringBuilder(); 
				 
				while ((line = reader.readLine()) != null) {
						sb.append(line);
				}
				
				backContent=sb.toString();
				
				reader.close();
				
				connection.disconnect();
			}
			catch (IOException e) {
				e.printStackTrace();
			}
			
			return backContent;
	    }
	
		public static String httpGet(String getUrl,String parm){
		  
		   String contents="";
		   String postur2=getUrl+"?"+parm; 
		   try
		   {
	            URL  url=null;
			    url = new URL(postur2);
			    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); 
			    httpURLConnection.setRequestMethod("GET");
			    httpURLConnection.connect(); 
			    if(parm.contains("serialno")){
			    	System.out.println(httpURLConnection.getHeaderField("serialno"));
			    }else{
			    	System.out.println(httpURLConnection.getHeaderField("cpparam"));
			    }
			    BufferedReader in = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream(),"UTF-8")); 
			    String line = ""; 
			    StringBuilder sb = new StringBuilder(); 
			    
			    while ((line = in.readLine()) != null){ 
			    	sb.append(line); 
			    }
			    
			    contents=sb.toString();
			    in.close(); 
			    httpURLConnection.disconnect();
		   }
		   catch(Exception e)
		   {
			   e.printStackTrace();
		   }
		 
		   return contents;
	   }

}

